﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;
using CodeRobot.Utility;
using System.Windows.Forms;

namespace CodeRobot.DAL
{
    /// <summary>
    /// Java 新增类
    /// </summary>
    public class JavaViewsCreateHelper
    {

        /// <summary>
        /// 根据表名创建Service类
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateViewCreateClass(string strFilePath,string strProjectName,string strHtmlInputList, string strTableName,string strTableComment)
        {
            //读取版权信息
            CodeRobot.Utility.IniFile iniFile = new Utility.IniFile(Application.StartupPath + "\\config.ini");
            string strCompany = iniFile.GetString("COPYRIGHT", "COMPANY", "");
            string strAuthor = iniFile.GetString("COPYRIGHT", "AUTHOR", "");
            string strVersion = iniFile.GetString("COPYRIGHT", "VERSION", "");
            string strCode = iniFile.GetString("COPYRIGHT", "CODE", "");
            string strCreateDate = iniFile.GetString("BASE", "CREATE_DATE", "");

            string strTableNameUpper = CommonHelper.GetTableNameUpper(strTableName);
            
            string strPrimaryKey = CommonHelper.GetPrimaryKey(strTableName);//如：news_id

            string strAllColumnName = CommonHelper.GetAllColumnName(strTableName);
            string strAllColumnNameNotKey = CommonHelper.GetAllColumnNameNotKey(strTableName);

            string strClassName = CommonHelper.GetClassName(strTableName);//类名
            strClassName = CommonHelper.GetTableNameUpper(strClassName);//News
            string strTableNameSpec = CommonHelper.GetTableNameFirstLowerSecondUpper(strClassName);//如：newsTypes
            string strTableNameLower = strTableNameSpec.ToLower();//如：newstypes

            string strNewPath = strFilePath + "\\resources\\static\\"+ strTableNameLower + "\\webpage";
            if (!Directory.Exists(strNewPath))
            {
                Directory.CreateDirectory(strNewPath);
            }
            
            StreamWriter sw = new StreamWriter(strNewPath + "\\" + strTableNameSpec + "Add.html", false, Encoding.GetEncoding("utf-8"));

            sw.WriteLine("<!DOCTYPE html>");
            sw.WriteLine("<html>");
            sw.WriteLine("<head>");
            sw.WriteLine("    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\">");
            sw.WriteLine("    <meta charset=\"utf-8\"/>");
            sw.WriteLine("    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, maximum-scale=1\">");
            sw.WriteLine("    <link rel=\"stylesheet\" href=\"../../../layuiadmin/layui/css/layui.css\" media=\"all\"/>");
            sw.WriteLine("    <link rel=\"stylesheet\" href=\"../../../layuiadmin/style/admin.css\" media=\"all\"/>");
            sw.WriteLine("    <link rel=\"stylesheet\" href=\"../../../css/common/loader.css\" media=\"all\"/>");
            sw.WriteLine("    <link rel=\"stylesheet\" href=\"../../../css/common/openshow.css\" media=\"all\"/>");
            sw.WriteLine("</head>");
            sw.WriteLine("<body>");
            sw.WriteLine("<form class=\"layui-form layui-form-pane\" action=\"\" id=\"saveForm\">");
            sw.WriteLine(strHtmlInputList);
            sw.WriteLine("    <!-- 隐藏域 -->");
            sw.WriteLine("    <div class=\"layui-form-item\" style=\"display:none;\">");
            sw.WriteLine("        <button class=\"layui-btn\" lay-submit=\"\" lay-filter=\"saveSubmit\">提交</button>");
            sw.WriteLine("    </div>");
            sw.WriteLine("</form>");
            sw.WriteLine("<script src=\"../../../layuiadmin/layui/layui.js\"></script>");
            sw.WriteLine("<script type=\"text/javascript\" src=\"../../../lib/jQuery-2.2.0.min.js\"></script>");
            sw.WriteLine("<script>");
            sw.WriteLine("    layui.config({");
            sw.WriteLine("        base: '../../../layuiadmin/' //静态资源所在路径");
            sw.WriteLine("    }).extend({");
            sw.WriteLine("        webplus: 'lib/webplus', //主入口模块");
            sw.WriteLine("    }).use(['form', 'webplus'], function () {");
            sw.WriteLine("        var webplus = layui.webplus;");
            sw.WriteLine("        webplus.initForm('"+ strTableNameSpec + "/save');");
            sw.WriteLine("    });");
            sw.WriteLine("</script>");
            sw.WriteLine("</body>");
            sw.WriteLine("</html>");

            sw.Close();

        }

        
    }
}
